fix: detect setState in useEffect for anonymous component callbacks passed to HOFs#2
Closed
fresh3nough wants to merge 1 commit intomainfrom
Closed
fix: detect setState in useEffect for anonymous component callbacks passed to HOFs#2fresh3nough wants to merge 1 commit intomainfrom
fresh3nough wants to merge 1 commit intomainfrom
Conversation
…assed to HOFs (facebook#35910) The react-hooks/set-state-in-effect ESLint rule was not detecting setState calls inside useEffect when the component was an anonymous callback passed to a higher-order function (e.g. const MyComponent = wrap(() => { ... })). Root cause: getFunctionName() in the compiler did not handle the HOF pattern (parent is CallExpression whose parent is VariableDeclarator), so the anonymous function was never identified as a component and never compiled/validated. Changes: - Add HOF pattern handling to getFunctionName() in Program.ts - Add CallExpression to the mayContainReactCode heuristic in RunReactCompiler.ts so HOF-wrapped components are not skipped - Add test cases for both set-state-in-effect and prop mutation detection in HOF-wrapped components Fixes: facebook#35910
Owner
Author
|
Closing in favor of a PR against the upstream repo (facebook/react). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
react-hooks/set-state-in-effectESLint rule was not detectingsetStatecalls insideuseEffectwhen the component was an anonymous callback passed to a higher-order function (e.g.const MyComponent = wrap(() => { ... })).Root Cause
getFunctionName()in the compiler'sProgram.tsdid not handle the HOF pattern where the parent is aCallExpressionwhose parent is aVariableDeclarator. This meant the anonymous function was never identified as a component and never compiled/validated by the lint rules.Changes
compiler/.../Entrypoint/Program.ts: Add HOF pattern handling togetFunctionName()soconst MyComponent = wrap(() => {})resolves the function name toMyComponentpackages/eslint-plugin-react-hooks/src/shared/RunReactCompiler.ts: AddCallExpressionto themayContainReactCodeheuristic so files with HOF-wrapped components are not skippedpackages/eslint-plugin-react-hooks/__tests__/ReactCompilerRuleTypescript-test.ts: Add test cases for bothset-state-in-effectand prop mutation detection in HOF-wrapped componentsTest Plan
All 5086 existing ESLint plugin tests pass, plus 4 new tests:
Fixes: facebook#35910